package other.up.modbus.util;

import java.util.Arrays;

public class CoderUtils {  
  
	  public static String gbEncoding(final String gbString) {   //gbString = "����"
          char[] utfBytes = gbString.toCharArray();   //utfBytes = [��, ��]
          String unicodeBytes = "";   
          for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {   
              String hexB = Integer.toHexString(utfBytes[byteIndex]);   //ת��Ϊ16���������ַ���
                if (hexB.length() <= 2) {   
                    hexB = "00" + hexB;   
               }   
               unicodeBytes = unicodeBytes + "\\u" + hexB;   
          }   
          System.out.println("unicodeBytes is: " + unicodeBytes);   
          return unicodeBytes;   
	  }


    /**
     *
     * Unicode转中文
     *
     *
     * */
	      public static String decodeUnicode(final String dataStr) {   
	         int start = 0;   
	         int end = 0;   
	         final StringBuffer buffer = new StringBuffer();   
	         while (start > -1) {   
	             end = dataStr.indexOf("\\u", start + 2);   
	             String charStr = "";   
	             if (end == -1) {   
	                 charStr = dataStr.substring(start + 2, dataStr.length());   
	             } else {   
	                 charStr = dataStr.substring(start + 2, end);   
	             }   
	             char letter = (char) Integer.parseInt(charStr, 16); // 16����parse�����ַ�����   
	             buffer.append(new Character(letter).toString());   
	             start = end;   
	         }   
	         return buffer.toString();   
	      }


    /**
     * 字符串转16进制
     * */
	      public static String str2HexStr(String str) {
	    	    char[] chars = "0123456789ABCDEF".toCharArray();
	    	    StringBuilder sb = new StringBuilder("");
	    	    byte[] bs = str.getBytes();
	    	    int bit;
	    	    for (int i = 0; i < bs.length; i++) {
	    	        bit = (bs[i] & 0x0f0) >> 4;
	    	        sb.append(chars[bit]);
	    	        bit = bs[i] & 0x0f;
	    	        sb.append(chars[bit]);
	    	        // sb.append(' ');
	    	    }
	    	    return sb.toString().trim();
	    	}
    /**
     *
     *  16进制转中文
     * */
	      public static String hexStr2Str(String hexStr) {
	    	    String str = "0123456789ABCDEF";
	    	    char[] hexs = hexStr.toCharArray();
	    	    byte[] bytes = new byte[hexStr.length() / 2];
	    	    int n;
	    	    for (int i = 0; i < bytes.length; i++) {
	    	        n = str.indexOf(hexs[2 * i]) * 16;
	    	        n += str.indexOf(hexs[2 * i + 1]);
	    	        bytes[i] = (byte) (n & 0xff);
	    	    }
	    	    return new String(bytes);
	    	}
	      
	      public static void main(String[] args) {
			
	    	 System.out.println(CoderUtils.str2HexStr("ahello"));
	    	 System.out.println(Arrays.toString("6168656C6C6F".getBytes()));
	    	 System.out.println(Arrays.toString("ahello".getBytes()));
	    	 System.out.println(CoderUtils.hexStr2Str("D7D4D0D0B3B5D0D0D5FEB4E5"));
			}
	      
	      //
		      
  
}  